
from .graphNodeManager import GraphNodeManager
class Project:
    _project_token_to_project={}
    def __init__(self, id:str, random_key:str, project_name:str, user):
        from .userManager import User
        self.id = id
        self.random_key = random_key
        self.project_name = project_name
        self.onlineRoutes = {}
        self.user = user
        assert isinstance(self.user, User) 
        self.is_websocket_running = False
        self.databases = {} # {database_name: {session_id: {msgs: []}}}
        # 改为{session_id: {database_name: {msgs: []}}}

    def stop(self):
        self.graphNodeManager.stop()

    def deployProject(self, data:dict):
        nodes = data['initialNodes']
        edges = data['initialEdges']
        self.graphNodeManager = GraphNodeManager(self)
        self.graphNodeManager.deploy(nodes, edges, self.random_key)
        Project._project_token_to_project[self.graphNodeManager.http_node.full_url] = self

    def addOnlineRoute(self, route, event_type:str=None, node_name=None): # event_type=audio or message or image or project_process
        self.onlineRoutes[route] = (event_type, node_name)

    def removeOnlineRoute(self, route):
        if route in self.onlineRoutes:
            del self.onlineRoutes[route]

    def register_project_process_route(self, route, node_name):
        self.onlineRoutes[route] = ("project_process", node_name)